package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bo.ArticleInfo;
import com.entity.Article;
import com.entity.Content;
import com.entity.User;
import com.repository.ArticleRepository;
import com.repository.ContentRepository;
import com.repository.UserRepository;
import com.service.IUserService;
import com.vo.PageBean;

@RestController
@RequestMapping("/api")
public class Contoller {

    @Autowired
    IUserService userService;

    @Autowired
    UserRepository userRep;

    @Autowired
    ArticleRepository articleRep;

    @Autowired
    ContentRepository contentRep;

    @PostMapping("/article")
    public ArticleInfo insertArticle(@RequestBody ArticleInfo article) {
        return userService.insertArtice(article);
    }

    @PostMapping("/user")
    public User insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @GetMapping("/user")
    public List<User> getUser() {
        return userRep.findAll();
    }

    @GetMapping("/user/{name}")
    public User getUser(@PathVariable String name) {
        return userRep.findByName(name);
    }

    @GetMapping("/article/{id}")
    public Article article(@PathVariable String id) {
        return articleRep.findById(id).get();
    }

    @GetMapping("/article/{pageIndex}/{pageSize}")
    public PageBean<Article> article(@PathVariable int pageIndex, @PathVariable int pageSize) {
        return userService.find(pageIndex, pageSize);
    }

    @GetMapping("/content/{articleId}")
    public Content content(@PathVariable String articleId) {
        return contentRep.findByArticleId(articleId);
    }
}
